<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>算力平台账户申请</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
            position: relative;
            overflow-x: hidden;
        }

        /* 背景动态效果 */
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-image: 
                radial-gradient(circle at 20% 50%, rgba(59, 130, 246, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(99, 102, 241, 0.1) 0%, transparent 50%),
                radial-gradient(circle at 40% 20%, rgba(139, 92, 246, 0.1) 0%, transparent 50%);
            pointer-events: none;
            animation: float 20s ease-in-out infinite;
        }

        @keyframes float {
            0%, 100% { transform: translate(0, 0) rotate(0deg); }
            33% { transform: translate(-20px, -20px) rotate(1deg); }
            66% { transform: translate(20px, -10px) rotate(-1deg); }
        }

        .container {
            background: rgba(255, 255, 255, 0.05);
            backdrop-filter: blur(20px);
            border-radius: 24px;
            padding: 40px;
            max-width: 500px;
            width: 100%;
            box-shadow: 
                0 20px 60px rgba(0, 0, 0, 0.3),
                0 0 0 1px rgba(255, 255, 255, 0.1),
                inset 0 0 0 1px rgba(255, 255, 255, 0.05);
            position: relative;
            animation: slideUp 0.6s ease-out;
        }

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .header {
            text-align: center;
            margin-bottom: 35px;
            position: relative;
        }

        /* Logo容器 - 用于图片logo */
        .logo-container {
            width: 100px;
            height: 100px;
            margin: 0 auto 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            
        }

        /* 真实的logo图片样式 */
        .logo {
            max-width: 100px;
            max-height: 100px;
            width: auto;
            height: auto;
        }

        /* Logo容器 - 用于图片logo */
        .logo3-container {
            width: 400px;
            height: 400px;
            margin: 0 auto 15px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* 真实的logo图片样式 */
        .logo3 {
            max-width: 400px;
            max-height: 400px;
            width: auto;
            height: auto;
        }
        /* 备用文字logo样式（当图片无法加载时显示） */
        .logo-fallback {
            width: 100px;
            height: 100px;
            background: linear-gradient(135deg, #3b82f6, #8b5cf6);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3);
        }

        .logo-text {
            color: white;
            font-size: 24px;
            font-weight: bold;
        }

        h1 {
            color: #ffffff;
            font-size: 24px;
            font-weight: 600;
            letter-spacing: 0.5px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
        }

        h2 {
            color: #e2e8f0;
            font-size: 18px;
            font-weight: 500;
            margin-bottom: 20px;
            text-align: center;
        }

        .user-type-selector {
            margin-bottom: 30px;
        }

        .type-buttons {
            display: flex;
            gap: 10px;
            background: rgba(30, 58, 138, 0.2);
            padding: 5px;
            border-radius: 12px;
        }

        .type-btn {
            flex: 1;
            padding: 12px 20px;
            background: transparent;
            border: none;
            color: #94a3b8;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            border-radius: 8px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .type-btn:hover {
            color: #ffffff;
            transform: translateY(-1px);
            background: rgba(59, 130, 246, 0.1);
        }

        .type-btn.active {
            background: linear-gradient(135deg, #3b82f6, #8b5cf6);
            color: #ffffff;
            box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3);
            position: relative;
            z-index: 1;
        }

        .type-btn.active span {
            position: relative;
            z-index: 2;
        }

        .form-container {
            animation: fadeIn 0.4s ease-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: scale(0.95);
            }
            to {
                opacity: 1;
                transform: scale(1);
            }
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            color: #cbd5e1;
            font-size: 14px;
            font-weight: 500;
            margin-bottom: 8px;
            letter-spacing: 0.3px;
        }

        input[type="text"],
        input[type="email"],
        input[type="password"] {
            width: 100%;
            padding: 12px 16px;
            background: rgba(15, 23, 42, 0.5);
            border: 1px solid rgba(148, 163, 184, 0.2);
            border-radius: 10px;
            color: #ffffff;
            font-size: 15px;
            transition: all 0.3s ease;
            backdrop-filter: blur(10px);
        }

        input::placeholder {
            color: #64748b;
        }

        input:focus {
            outline: none;
            border-color: #3b82f6;
            background: rgba(15, 23, 42, 0.7);
            box-shadow: 
                0 0 0 3px rgba(59, 130, 246, 0.1),
                0 4px 12px rgba(59, 130, 246, 0.2);
            transform: translateY(-1px);
        }

        button[type="submit"] {
            width: 100%;
            padding: 14px 24px;
            background: linear-gradient(135deg, #3b82f6, #8b5cf6);
            border: none;
            border-radius: 10px;
            color: #ffffff;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3);
            position: relative;
            overflow: hidden;
            margin-top: 25px;
        }

        button[type="submit"]::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            transition: left 0.5s ease;
        }

        button[type="submit"]:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(59, 130, 246, 0.4);
        }

        button[type="submit"]:hover::before {
            left: 100%;
        }

        button[type="submit"]:active {
            transform: translateY(0);
        }

        .admin-link {
            text-align: center;
            margin-top: 20px;
            padding-top: 20px;
            border-top: 1px solid rgba(148, 163, 184, 0.1);
        }

        .admin-link a {
            color: #60a5fa;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s ease;
            display: inline-block;
            position: relative;
        }

        .admin-link a::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 0;
            height: 2px;
            background: linear-gradient(90deg, #3b82f6, #8b5cf6);
            transition: width 0.3s ease;
        }

        .admin-link a:hover {
            color: #93c5fd;
            transform: translateY(-1px);
        }

        .admin-link a:hover::after {
            width: 100%;
        }

        /* 响应式设计 */
        @media (max-width: 640px) {
            .container {
                padding: 30px 20px;
            }

            h1 {
                font-size: 20px;
            }

            .type-buttons {
                flex-direction: column;
            }

            .type-btn {
                width: 100%;
            }
        }

        /* 加载动画 */
        @keyframes pulse {
            0%, 100% {
                opacity: 1;
            }
            50% {
                opacity: 0.5;
            }
        }

        /* 成功/错误提示样式 */
        .alert {
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
            animation: slideDown 0.3s ease-out;
        }

        @keyframes slideDown {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .alert-success {
            background: rgba(34, 197, 94, 0.1);
            border: 1px solid rgba(34, 197, 94, 0.3);
            color: #86efac;
        }

        .alert-error {
            background: rgba(239, 68, 68, 0.1);
            border: 1px solid rgba(239, 68, 68, 0.3);
            color: #fca5a5;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <!-- Logo容器 -->
            <div class="logo-container">
                <!-- 使用Flask的url_for生成正确的静态文件路径 -->
                <img src="{{ url_for('static', filename='images/logo.png') }}" 
                     alt="算力平台Logo" 
                     class="logo"
                     onerror="this.style.display='none'; document.getElementById('logoFallback').style.display='flex';">
                <!-- 备用文字logo（当图片加载失败时显示） -->
                <!-- <div id="logoFallback" class="logo-fallback" style="display: none;">
                    <span class="logo-text">算力</span>
                </div> -->
            </div>
            <!-- <div class="logo3-container">
                <img src="{{ url_for('static', filename='images/logo3.png') }}" alt="Logo" class="logo3">
            </div> -->
            <h1>算力平台账户申请系统</h1>
        </div>

        <!-- 用户类型选择 - 用户登录放在第一个 -->
        <div class="user-type-selector">
            <h2>请选择操作类型</h2>
            <div class="type-buttons">
                <button id="loginBtn" class="type-btn active" onclick="showLoginForm()">用户登录</button>
                <button id="studentBtn" class="type-btn" onclick="showStudentForm()">学生申请</button>
                <button id="supervisorBtn" class="type-btn" onclick="showSupervisorForm()">导师申请</button>
            </div>
        </div>

        <!-- 登录表单 - 默认显示 -->
        <div id="loginForm" class="form-container">
            <!-- <h2>用户登录</h2> -->
            <form id="loginFormElement">
                <div class="form-group">
                    <label for="loginUsername">用户名</label>
                    <input type="text" id="loginUsername" name="username" placeholder="请输入用户名" required>
                </div>
                
                <div class="form-group">
                    <label for="loginPassword">密码</label>
                    <input type="password" id="loginPassword" name="password" placeholder="请输入密码" required>
                </div>
                
                <button type="submit">登录</button>
            </form>
            
            <div class="admin-link">
                <a href="/admin/login">管理员登录</a>
            </div>
        </div>

        <!-- 学生注册表单 -->
        <div id="studentForm" class="form-container" style="display: none;">
            <!-- <h2>学生账户申请</h2> -->
            <form id="studentRegistrationForm">
                <div class="form-group">
                    <label for="studentName">姓名</label>
                    <input type="text" id="studentName" name="name" placeholder="请输入您的姓名" required>
                </div>
                
                <div class="form-group">
                    <label for="studentId">学号</label>
                    <input type="text" id="studentId" name="student_id" placeholder="请输入您的学号" required>
                </div>
                
                <div class="form-group">
                    <label for="studentEmail">邮箱</label>
                    <input type="email" id="studentEmail" name="email" placeholder="example@email.com" required>
                </div>
                
                <div class="form-group">
                    <label for="supervisorEmail">导师邮箱</label>
                    <input type="email" id="supervisorEmail" name="supervisor_email" placeholder="supervisor@email.com" required>
                </div>
                
                <div class="form-group">
                    <label for="studentSupervisor">导师姓名</label>
                    <input type="text" id="studentSupervisor" name="supervisor" placeholder="请输入导师姓名" required>
                </div>
                
                <div class="form-group">
                    <label for="studentUsername">期望用户名</label>
                    <input type="text" id="studentUsername" name="username" placeholder="请输入期望的用户名" required>
                </div>
                
                <div class="form-group">
                    <label for="studentPassword">期望密码</label>
                    <input type="password" id="studentPassword" name="password" placeholder="请输入期望的密码" required>
                </div>
                
                <button type="submit">提交学生申请</button>
            </form>
        </div>

        <!-- 导师注册表单 -->
        <div id="supervisorForm" class="form-container" style="display: none;">
            <!-- <h2>导师账户申请</h2> -->
            <form id="supervisorRegistrationForm">
                <div class="form-group">
                    <label for="supervisorName">姓名</label>
                    <input type="text" id="supervisorName" name="name" placeholder="请输入您的姓名" required>
                </div>
                
                <div class="form-group">
                    <label for="supervisorEmailReg">邮箱</label>
                    <input type="email" id="supervisorEmailReg" name="email" placeholder="example@email.com" required>
                </div>
                
                <div class="form-group">
                    <label for="department">院系</label>
                    <input type="text" id="department" name="department" placeholder="请输入所在院系" required>
                </div>
                
                <div class="form-group">
                    <label for="supervisorUsernameReg">期望用户名</label>
                    <input type="text" id="supervisorUsernameReg" name="username" placeholder="请输入期望的用户名" required>
                </div>
                
                <div class="form-group">
                    <label for="supervisorPasswordReg">期望密码</label>
                    <input type="password" id="supervisorPasswordReg" name="password" placeholder="请输入期望的密码" required>
                </div>
                
                <button type="submit">提交导师申请</button>
            </form>
        </div>
    </div>

    <script>
        // 切换表单显示 (这部分函数保持不变)
        function showLoginForm() {
            document.getElementById('loginForm').style.display = 'block';
            document.getElementById('studentForm').style.display = 'none';
            document.getElementById('supervisorForm').style.display = 'none';
            
            document.getElementById('loginBtn').classList.add('active');
            document.getElementById('studentBtn').classList.remove('active');
            document.getElementById('supervisorBtn').classList.remove('active');
        }
    
        function showStudentForm() {
            document.getElementById('studentForm').style.display = 'block';
            document.getElementById('supervisorForm').style.display = 'none';
            document.getElementById('loginForm').style.display = 'none';
            
            document.getElementById('studentBtn').classList.add('active');
            document.getElementById('supervisorBtn').classList.remove('active');
            document.getElementById('loginBtn').classList.remove('active');
        }
    
        function showSupervisorForm() {
            document.getElementById('studentForm').style.display = 'none';
            document.getElementById('supervisorForm').style.display = 'block';
            document.getElementById('loginForm').style.display = 'none';
            
            document.getElementById('studentBtn').classList.remove('active');
            document.getElementById('supervisorBtn').classList.add('active');
            document.getElementById('loginBtn').classList.remove('active');
        }
    
        // --- 表单提交处理 (已加入验证逻辑) ---
    
        // 处理登录表单提交
        document.getElementById('loginFormElement').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const data = {
                username: formData.get('username'),
                password: formData.get('password')
            };
            
            // --- 新增：用户名格式验证 ---
            const usernameRegex = /^[a-zA-Z0-9]+$/;
            if (!usernameRegex.test(data.username)) {
                alert('登录失败：用户名只能包含字母和数字！');
                return; // 验证失败，中止提交
            }
            
            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    alert(result.message);
                    if (result.redirect) {
                        window.location.href = result.redirect;
                    }
                } else {
                    alert(result.error || '登录失败');
                }
            } catch (error) {
                alert('网络错误，请稍后重试');
                console.error('Error:', error);
            }
        });
    
        // 处理学生注册表单提交
        document.getElementById('studentRegistrationForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const data = {
                user_type: 'student',
                name: formData.get('name'),
                student_id: formData.get('student_id'),
                email: formData.get('email'),
                supervisor_email: formData.get('supervisor_email'),
                supervisor: formData.get('supervisor'),
                username: formData.get('username'),
                password: formData.get('password')
            };
            
            // --- 新增：邮箱格式验证 ---
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(data.email) || !emailRegex.test(data.supervisor_email)) {
                alert('申请失败：请输入有效的邮箱地址（学生邮箱和导师邮箱）！');
                return; // 验证失败，中止提交
            }

            // --- 修改：用户名格式验证 (全小写字母+数字，且至少1个字母) ---
            const usernameRegex = /^(?=.*[a-z])[a-z0-9]+$/;
            if (!usernameRegex.test(data.username)) {
                alert('申请失败：期望用户名只能由小写字母和数字组成，且必须包含至少一个字母！');
                return; // 验证失败，中止提交
            }
            try {
                const response = await fetch('/api/register', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    alert(result.message);
                    this.reset();
                } else {
                    alert(result.error || '注册失败');
                }
            } catch (error) {
                alert('网络错误，请稍后重试');
                console.error('Error:', error);
            }
        });
    
        // 处理导师注册表单提交
        document.getElementById('supervisorRegistrationForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const data = {
                user_type: 'supervisor',
                name: formData.get('name'),
                email: formData.get('email'),
                department: formData.get('department'),
                username: formData.get('username'),
                password: formData.get('password')
            };
            
            // --- 新增：邮箱格式验证 ---
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(data.email)) {
                alert('申请失败：请输入有效的邮箱地址！');
                return; // 验证失败，中止提交
            }
            
            // --- 修改：导师用户名格式验证 (全小写字母+数字，且至少1个字母) ---
            const usernameRegex = /^(?=.*[a-z])[a-z0-9]+$/;
            if (!usernameRegex.test(data.username)) {
                alert('申请失败：期望用户名只能由小写字母和数字组成，且必须包含至少一个字母！');
                return; // 验证失败，中止提交
            }
            
            try {
                const response = await fetch('/api/register', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                
                if (response.ok) {
                    alert(result.message);
                    this.reset();
                } else {
                    alert(result.error || '注册失败');
                }
            } catch (error) {
                alert('网络错误，请稍后重试');
                console.error('Error:', error);
            }
        });
    
        // 输入框聚焦效果增强 (保持不变)
        document.querySelectorAll('input').forEach(input => {
            input.addEventListener('focus', function() {
                // 平滑过渡效果
                this.parentElement.style.transition = 'transform 0.2s ease-in-out';
                this.parentElement.style.transform = 'scale(1.02)';
            });
            
            input.addEventListener('blur', function() {
                this.parentElement.style.transform = 'scale(1)';
            });
        });
    
        // 默认显示登录表单
        showLoginForm();
    </script>


<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">

<script src="{{ url_for('static', filename='js/script.js') }}"></script>
</body>
</html>
